package com.gin.kotlin.gf2gamecommunity.composable

import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.AssistChip
import androidx.compose.material3.AssistChipDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.viewmodel.UserFollowViewModel

@Composable
fun FollowChip(uid: Int, value: Boolean, viewModel: UserFollowViewModel = viewModel(), modifier: Modifier = Modifier, onSuccess: (followed: Boolean) -> Unit) {
    var followed by remember(uid) { mutableStateOf(value) }
    AssistChip(onClick = {
        viewModel.follow(uid) {
            App.toast(it)
            followed = !followed
            onSuccess(followed)
        }
    }, label = { Text(if (followed) "已关注" else "未关注") }, leadingIcon = {
        Icon(if (followed) Icons.Filled.Check else Icons.Filled.Add, contentDescription = null, Modifier.size(AssistChipDefaults.IconSize))
    })
}